-module(echo).
-export([go/0, loop/0]).

go() ->
    %Pid2 = spawn(echo, loop, []),
    Pid2 = spawn(fun() -> loop() end),
    register(pong, Pid2),
    pong ! {self(), hello},

    % sleep 10 ms
    receive
    after 10 -> true 
    end,

    % receive No.2 message 
    receive
        {Pid2, 2, Msg} ->
            io:format("P1 2:~w~n", [Msg])
    after 0 -> 
        receive
            {Pid2, N, Msg} -> io:format("P1 ~w:~w ~n", [N, Msg])
        end
    end,
    pong ! stop.

loop() ->
    receive
        {From, Msg} ->
            From ! {self(), 1, Msg},
            From ! {self(), 2, Msg},
            loop();
        stop ->
            true
    end.
